﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.Text;
using SharePointClient.SPWebService.Views;

namespace SharePointClient
{
    /// <summary>
    /// Collection of Views for ONE List
    /// </summary>
    public class ViewCollection : WSSBase, IDisposable,IEnumerable<View>
    {
        private List<View> views = new List<View>();
        private Guid listID;

        /// <summary>
        /// Creates a new Collection of Views
        /// </summary>
        /// <param name="url">Base-Url of LIST</param>
        /// <param name="listID">LIST-Identity</param>
        internal ViewCollection(Connection connection,Uri url, Guid listID)
            : base(connection,url)
        {
            this.listID = listID;
            Refresh();
        }
        /// <summary>
        /// Count of Views in Collection
        /// </summary>
        public int Count
        {
            get { return views.Count; }
        }
        /// <summary>
        /// Guid of List
        /// </summary>
        public Guid ListGuid
        {
            get { return listID; }
        }
        /// <summary>
        /// Get View by Position in Collection
        /// </summary>
        /// <param name="pos">Position</param>
        /// <returns>View of List</returns>
        public View this[int pos]
        {
            get { return views[pos]; }
        }
        /// <summary>
        /// Get View by Displayname
        /// </summary>
        /// <param name="displayname">DislayName of View</param>
        /// <returns>View of List</returns>
        public View this[string displayname]
        {
            get
            {
                displayname = displayname.ToLower();
                foreach (View v in views)
                    if (v.Title.ToLower() == displayname)
                        return v;
                return null;
            }
        }
        /// <summary>
        /// Get View by Guid of View
        /// </summary>
        /// <param name="name">Guid of View</param>
        /// <returns>View of List</returns>
        public View this[Guid name]
        {
            get
            {
                foreach (View v in views)
                    if (v.Guid == name)
                        return v;
                return null;
            }
        }

        /// <summary>
        /// Refreshes Information from Sharepoint
        /// </summary>
        public override void Refresh()
        {
            Views viewsweb = new Views();
            viewsweb.Url = Uri + View.WebService;
            viewsweb.Credentials = Credentials;
            ProgressEventArgs p =myConnection.DoProgress("Loading ViewCollection \""+Uri.ToString()+"\".");
            MetaDataElement = viewsweb.GetViewCollection(listID.ToString());
            XmlNodeList list = Select("//wss:View");
            foreach (XmlNode viewnode in list)
            {
                Guid guid = new Guid(Select(viewnode, "@ID")[0].Value);
                View v;
                if (myConnection.Cached(guid))
                {
                    v = (View) myConnection[guid];
                }
                else
                    v = new View(myConnection, Uri, listID, viewnode);
                views.Add(v);
            }
            p.Operation = "Loaded " + views.Count + " from ViewCollection at \"" + Uri.ToString() + "\".";
            p.State = BeforeOrAfter.After;
            myConnection.UpdateProgress(p);
            viewsweb.Dispose();
            NeedRefresh = false;
        }
        /// <summary>
        /// Returns View-DisplayName instead of ClassName
        /// </summary>
        /// <returns>DisplayName</returns>
        public override string ToString()
        {
            string sview = "";
            foreach (View v in views)
                sview += ",\"" + v.Title + "\"";
            if (sview != "") return sview.Substring(1);
            else return base.ToString();
        }

        #region IDisposable Member
        /// <summary>
        /// End of ViewCollection
        /// </summary>
        public void Dispose()
        {
            foreach (View v in views) v.Dispose();
            views.Clear();
        }

        #endregion

        #region IEnumerable<View> Member

        public IEnumerator<View> GetEnumerator()
        {
            return views.GetEnumerator();
        }

        #endregion

        #region IEnumerable Member

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return views.GetEnumerator();
        }

        #endregion
    }
}
